<template>
    <div>
        <Title :title="title"></Title>
        <!-- {{ ed }} -->
        <div ref="right1" style="height: 150px;margin-top: 10px;"></div>
    </div>
</template>
<script>
import { get } from '@/utils/request';
import { DualAxes } from '@antv/g2plot';
import moment from 'moment';
import { mapState } from 'vuex';

export default {
    data() {
        return {
            title: '实时湿度曲线',
            params: {
                page: 1,
                pageSize: 100
            },
            // 存放湿度数据
            humData: [],
            line: null
        }
    },

    computed: {
        ...mapState(['ed'])
    },
    watch: {
        ed: {
            handler() {
                this.getHumData();
                this.DualAxes.changeData(this.humData);
            },
            deep: true
        }
    },
    methods: {
        loadLine() {

            this.dualAxes = new DualAxes(this.$refs.right1, {
                // 将后端获取数据设置给data属性
                // data: [data, data],
                data: [this.humData, this.humData],
                xField: 'insert_time',
                yField: ['temperature', 'humidity'],
                geometryOptions: [
                    {
                        geometry: 'line',
                        color: '#5B8FF9',
                    },
                    {
                        geometry: 'line',
                        color: '#5AD8A6',
                    },
                ],
            });

            this.dualAxes.render();
        },
        async getHumData() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            let hum = [];
            res.data.list.forEach((item, index, arr) => {
                 arr[0].temperature = 0;
                item.insert_time = moment(item.insert_time).format("HH:mm");
                if (index % 10 == 0) {
                    hum.push(arr[index])
                }
            });
            this.humData = hum;
        },

    },

    async mounted() {
        await this.getHumData();
        this.loadLine()
    }

}
</script>
<style lang="less" scoped></style>